package com.yike.user.service.impl;

import com.yike.common.exception.BusinessException;
import com.yike.common.tool.LoginUtil;
import com.yike.user.entity.PlatformRole;
import com.yike.user.mapper.PlatformRoleMapper;
import com.yike.user.param.PlatformRoleParam;
import com.yike.user.service.PlatformRoleService;
import com.yike.user.param.PlatformRolePageParam;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yike.common.base.BaseServiceImpl;
import com.yike.common.pagination.Paging;
import com.yike.common.pagination.PageInfo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yike.user.vo.PlatformRoleVo;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 平台角色 服务实现类
 *
 * @author Sakura
 * @since 2024-10-29
 */
@Slf4j
@Service
public class PlatformRoleServiceImpl extends BaseServiceImpl<PlatformRoleMapper, PlatformRole> implements PlatformRoleService {

    @Autowired
    private PlatformRoleMapper platformRoleMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean savePlatformRole(PlatformRoleParam platformRoleParam) throws Exception {
        // 校验名称是否重复
        PlatformRole platformRole = platformRoleMapper.selectOne(new LambdaQueryWrapper<PlatformRole>()
                .eq(PlatformRole::getName, platformRoleParam.getName()));
        if (platformRole != null) {
            throw new BusinessException("当前角色已存在，请勿重复添加");
        }
        platformRole = new PlatformRole();
        BeanUtils.copyProperties(platformRoleParam, platformRole);
        platformRole.setCreateBy(LoginUtil.getUserId());
        return super.save(platformRole);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updatePlatformRole(PlatformRoleParam platformRoleParam) throws Exception {
        // 获取原角色信息
        PlatformRole platformRole = platformRoleMapper.selectById(platformRoleParam.getId());
        if (platformRole == null) {
            throw new BusinessException("角色信息异常");
        }
        // 如果ID为1是系统默认角色无法修改
        if (platformRole.getId() == 1L) {
            throw new BusinessException("系统默认角色无法修改");
        }
        // 校验修改名称是否已存在
        Integer count = platformRoleMapper.selectCount(new LambdaQueryWrapper<PlatformRole>()
                .eq(PlatformRole::getName, platformRoleParam.getName())
                .ne(PlatformRole::getId, platformRoleParam.getId()));
        if (count > 0) {
            throw new BusinessException("当前角色已存在，请勿重复添加");
        }
        BeanUtils.copyProperties(platformRoleParam, platformRole);
        platformRole.setUpdateBy(LoginUtil.getUserId());
        return super.updateById(platformRole);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deletePlatformRole(Long id) throws Exception {
        // 如果ID为1则为系统默认角色无法删除
        if (id == 1L) {
            throw new BusinessException("系统默认角色无法删除");
        }
        return super.removeById(id);
    }

    @Override
    public PlatformRoleVo getPlatformRole(Long id) throws Exception {
        return platformRoleMapper.getPlatformRoleVo(id);
    }

    @Override
    public Paging<PlatformRoleVo> getPlatformRolePageList(PlatformRolePageParam platformRolePageParam) throws Exception {
        Page<PlatformRole> page = new PageInfo<>(platformRolePageParam);
        IPage<PlatformRoleVo> iPage = platformRoleMapper.getPlatformRoleList(page, platformRolePageParam);
        return new Paging<PlatformRoleVo>(iPage);
    }

}
